package com.csdj.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * packageName com.csdj.util
 *
 * @author winver
 * @version 1.0
 * @className ParamsTest
 * @date 2024/11/29
 * @description TODO
 */

public class ParamsTest {

    /**
     * 可变参数方法
     *
     * @param params [传0~无数个都可以]
     */
    public static void test1(Object... params) {
        //方法体内部处理时， 按数组模式处理
        for (Object param : params) {
            System.out.println("参数：" + param);
        }
    }

    public static void test3(Object[] params) {
        //方法体内部处理时， 按数组模式处理
        for (Object param : params) {
            System.out.println("参数：" + param);
        }
    }

    /**
     * 定义可变参数方法时 只有一个可变参数对象  只能放后面，
     *
     * @param age
     * @param params
     */
    public static void test2(int age, Object... params) {
        //方法体内部处理时， 按数组模式处理
        for (Object param : params) {
            System.out.println("参数：" + param);
        }
    }

    public static void main(String[] args) {
       /* test1();
        System.out.println("===============================");
        test1(1);
        System.out.println("===============================");
        test1("a");
        System.out.println("===============================");
        test1(1,2,3,4,5,6,7,8,9,10);
        System.out.println("===============================");

        int[] arr = {1,3,5,7,9};
        test1(arr);*/

        Object[] arrs = {1, 2, 3, 4, 5, 6, 7, 8, "a", "b", "c"};
        test1(1, 2, 3, 4, 5, 6, 7, 8, "a", "b", "c");
        System.out.println("===============================");
        //数组转集合
        List<Object> arrList = Arrays.asList(arrs);

        for (Object o : arrList) {
            System.out.println("o:" + o);
        }

        //arrList.add("d");
        //集合转数组
        Object[] objs = arrList.toArray();


        List<Object> myList = new ArrayList<>();
        myList.add("abc");
        myList.forEach(System.out::println);
        System.out.println("===============================");

        List<Object> newMyList = Collections.unmodifiableList(myList);
        myList.add("d");
        newMyList.forEach(obj -> {
            System.out.println(obj+"------------------");
        });
        System.out.println("===============================");

        //不可变集合
        List<Object> myList1 = List.of("a", "b", "c");
        myList1.forEach(obj -> {
            System.out.println(obj);
        });
        //myList1.add("d"); //出异常
    }
}
